package com.qqin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qqin.mapper.*;
import com.qqin.pojo.Check;
import com.qqin.pojo.Goods;
import com.qqin.pojo.PageBean;
import com.qqin.service.CheckService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

@Service
public class CheckServiceImpl implements CheckService {
    private AddressMapper addressMapper;
    private CheckMapper checkMapper;
    private GoodsMapper goodsMapper;
    private CommentMapper commentMapper;
    private ItemMapper itemMapper;
    private OrderMapper orderMapper;
    private UsersMapper usersMapper;
    private SqlSessionFactory sqlSessionFactory;
    private SqlSession sqlSession;

    public CheckServiceImpl() {
        try {
            // 加载数据库配置文件
            Properties properties = new Properties();
            InputStream dbStream = Resources.getResourceAsStream("db.properties");
            properties.load(dbStream);

            // 加载MyBatis配置文件
            String resource = "mybatis-config.xml";
            InputStream mybatisStream = Resources.getResourceAsStream(resource);

            // 创建SqlSessionFactory
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(mybatisStream, properties);

            // 获取SqlSession
            sqlSession = sqlSessionFactory.openSession();

            // 获取Mapper实例
            checkMapper = sqlSession.getMapper(CheckMapper.class);
            goodsMapper= sqlSession.getMapper(GoodsMapper.class);
            commentMapper= sqlSession.getMapper(CommentMapper.class);
            itemMapper= sqlSession.getMapper(ItemMapper.class);
            orderMapper= sqlSession.getMapper(OrderMapper.class);
            usersMapper= sqlSession.getMapper(UsersMapper.class);
            addressMapper= sqlSession.getMapper(AddressMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public PageBean page(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        List<Check> checks = checkMapper.selectAll();
        Page<Check> p = (Page<Check>)checks;

        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public Check selectById(Integer checkId) {
        return checkMapper.selectById(checkId);
    }

    @Override
    public PageBean selectByUser(Integer userId, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        List<Goods> goods = checkMapper.selectByUser(userId);
        Page<Goods> p = (Page<Goods>)goods;

        return new PageBean(p.getTotal(), p.getResult());
    }

    @Override
    public void changeState(Check check) {

        Integer goodsId = check.getCheckGoods();
        Goods goods = goodsMapper.selectById(goodsId);

        if (goods.getState() == 2) {
            System.out.println("该商品已售，无需继续审核");
        }

        if (check.getState() == 1) {
            goodsMapper.sale(goodsId);

        } else if (check.getState() == 2) {
            goodsMapper.delisting(goodsId);
        }

        checkMapper.updateById(check);
        sqlSession.commit();
    }

}
